package com.whfc.hdc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.hdc.dao.AlgDeviceDataLogMapper;
import com.whfc.hdc.dao.AlgDeviceDataMapper;
import com.whfc.hdc.dao.AppDeviceMapper;
import com.whfc.hdc.dto.AlgDeviceDataDTO;
import com.whfc.hdc.entity.AlgDeviceData;
import com.whfc.hdc.entity.AlgDeviceDataLog;
import com.whfc.hdc.entity.AppDevice;
import com.whfc.hdc.params.AlgDeviceDataLogParam;
import com.whfc.hdc.service.AlgDeviceDataService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-07-19 17:16
 */
@DubboService(interfaceClass = AlgDeviceDataService.class, version = "1.0.0")
public class AlgDeviceDataServiceImpl implements AlgDeviceDataService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppDeviceMapper appDeviceMapper;

    @Autowired
    private AlgDeviceDataMapper algDeviceDataMapper;

    @Autowired
    private AlgDeviceDataLogMapper algDeviceDataLogMapper;

    @Override
    public void init(String sn) throws BizException {
        logger.info("算法盒子硬件初始化, sn:{}", sn);
        AppDevice device = appDeviceMapper.selectBySn(sn);
        if (device == null) {
            logger.warn("未找到该硬件,sn:{}", sn);
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "未找到该硬件");
        }
        AlgDeviceData algDeviceData = algDeviceDataMapper.selectBySn(sn);
        if (algDeviceData != null) {
            return;
        }
        algDeviceData = new AlgDeviceData();
        algDeviceData.setDeviceId(device.getId());
        algDeviceData.setSn(device.getSn());
        algDeviceData.setCode(device.getCode());
        algDeviceDataMapper.insertSelective(algDeviceData);
    }

    @Override
    public PageData<AlgDeviceDataDTO> list(Integer pageNum, Integer pageSize, String keyword) throws BizException {
        logger.info("获取算法盒子列表, pageNum:{}, pageSize:{}, keyword:{}", pageNum, pageSize, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<AlgDeviceDataDTO> list = algDeviceDataMapper.selectDeviceList(keyword);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public PageData<AlgDeviceDataDTO> getDataLog(String sn, Integer pageNum, Integer pageSize, Date startTime, Date endTime) throws BizException {
        logger.info("获取算法盒子历史数据, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}",
                sn, pageNum, pageSize, startTime, endTime);
        PageHelper.startPage(pageNum, pageSize);
        List<AlgDeviceDataDTO> list = algDeviceDataLogMapper.selectDataLogList(sn, startTime, endTime);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void saveDataLog(AlgDeviceDataLogParam param) throws BizException {
        logger.info("保存算法盒子历史数据, param:{}", param);
        AlgDeviceDataLog dataLog = new AlgDeviceDataLog();
        BeanUtils.copyProperties(param, dataLog);
        Integer type = param.getType();
        //查询算法类型信息
        dataLog.setCheckType(type);
        dataLog.setCheckTypeName(param.getTypeName());
        //保存历史数据
        algDeviceDataLogMapper.insertSelective(dataLog);
        //更新算法盒子数据信息
        AlgDeviceData deviceData = algDeviceDataMapper.selectBySn(param.getSn());
        if (deviceData == null) {
            return;
        }
        deviceData.setTime(param.getTime());
        algDeviceDataMapper.updateByPrimaryKeySelective(deviceData);
    }
}
